package servlet;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;

import dao.*;
import model.*;

/**
 * Servlet implementation class feedbackData
 */
public class feedbackData extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProfileDAO pdao;
	ObjectDAO oDao;
	EmployeeDAO emDao;
	FeedbackDAO fDao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public feedbackData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		ArrayList<Feedback> list = new ArrayList<Feedback>();
		Writer out = response.getWriter();
		String jsonString = "";
		
		/**
		 * cast data from database into json to present on Jqgrid in view
		 */
		
		try {
			if(!session.isNew()) {
				pdao = new ProfileDAO();
				fDao = new FeedbackDAO();
				emDao = new EmployeeDAO();
				oDao = new ObjectDAO();
				
				//Employee em = (Employee) session.getAttribute("person");
				list = fDao.listFeedbacks();
				if(!list.isEmpty()) {
					int recNo = list.size();
					
					JSONArray cellArray = new JSONArray();
					
					for(Feedback fb : list) {
						Category ca = oDao.getACategory(fb.getSection().getCate().getCategoryId());
						
						JSONObject cellObj = new JSONObject();
						cellObj.put("id", fb.getFeedbackId());
						cellObj.put("content", fb.getContent());
						cellObj.put("owner", fb.getOwner().getUsername());
						cellObj.put("target", fb.getTarget().getUsername());
						cellObj.put("date", fb.getDateMade().toString());
						cellObj.put("section", fb.getSection().getSectionName());
						cellObj.put("category", ca.getCategoryName());
						cellArray.put(cellObj);
					}
					
					jsonString = "{ \"page\": \"1\", \"record\": \"" + recNo + "\", \"rows\": ";
					jsonString += ( cellArray.toString() + " }"); 

					out.write(jsonString);
					out.flush();
				}
			}
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
